<?php
/**
*Copyright:D5game.com.cn
*Date:2011-3-3
*Author:Yuanqi
*/
include_once('./common_admin.php');
$op = $_GET['op'];
if($op == 'logout') {
	
	if($_GET['uhash'] == $_SGLOBAL['uhash']) {
		//ɾ��session
		if($_SGLOBAL['supe_uid']) {
			$_SGLOBAL['db']->query("DELETE FROM ".tname('session')." WHERE uid='$_SGLOBAL[supe_uid]'");
			$_SGLOBAL['db']->query("DELETE FROM ".tname('adminsession')." WHERE uid='$_SGLOBAL[supe_uid]'");//����ƽ̨
		}
	
//		if($_SCONFIG['uc_status']) {
//			include_once S_ROOT.'./uc_client/client.php';
//			$ucsynlogout = uc_user_synlogout();
//		} else {
//			$ucsynlogout = '';
//		}
	
		clearcookie();
		ssetcookie('_refer', '');
	}
	redirect('adm_login.php');

} 

include_once(S_ROOT.'./source/function_cp.php');
/*
if($_SGLOBAL['supe_uid']) {
	showmessage('do_success', 'admincp.php?ac=index', 0);
}
*/
//$refer = empty($_GET['refer'])?rawurldecode($_SCOOKIE['_refer']):$_GET['refer'];
//preg_match("/(admincp|do|cp)\.php\?ac\=([a-z]+)/i", $refer, $ms);
//if($ms) {
//	if($ms[1] != 'cp' || $ms[2] != 'sendmail') $refer = '';
//}
//if(empty($refer)) {
//	$refer = 'admincp.php?ac=index';
//}

//��������
//$uid = empty($_GET['uid'])?0:intval($_GET['uid']);

//û�е�¼�?
//$_SGLOBAL['nologinform'] = 1;

if(submitcheck('loginsubmit')) {

	$password = $_POST['password'];
	$username = trim($_POST['username']);
	$cookietime = intval($_POST['cookietime']);
	
//	$cookiecheck = $cookietime?' checked':'';
//	$membername = $username;
	
	if(empty($_POST['username'])) {
		showmessage('users_were_not_empty_please_re_login', 'do.php?ac='.$_SCONFIG['login_action']);
	}
	
	if($_SCONFIG['seccode_login']) {
		include_once(S_ROOT.'./source/function_cp.php');
		if(!ckseccode($_POST['seccode'])) {
			$_SGLOBAL['input_seccode'] = 1;
			include template('do_login');
			exit;
		}
	}
	//ͬ����ȡ�û�Դ
	if(!$passport=checkadmin($username, $password)) {
		showmessage('login_failure_please_re_login', 'adm_login.php');
	}
	
//	$setarr = array(
//		'uid' => $passport['uid'],
//		'username' => addslashes($passport['username']),
//		'password' => md5("$passport[uid]|$_SGLOBAL[timestamp]")//��������������
//	);
//	
//	include_once(S_ROOT.'./source/function_space.php');
//	//����ǰ�û�
//	$query = $_SGLOBAL['db']->query("SELECT password FROM ".tname('member')." WHERE uid='$setarr[uid]'");
//	if($value = $_SGLOBAL['db']->fetch_array($query)) {
//		$setarr['password'] = addslashes($value['password']);
//	} else {
//		//���±����û���
//		inserttable('member', $setarr, 0, true);
//	}
//
//	//��������session
//	insertsession($setarr);
	
	//����cookie
	ssetcookie('auth', authcode("$passport[password]\t$passport[uid]", 'ENCODE'), $cookietime);
	ssetcookie('loginuser', $passport['username'], 31536000);
	ssetcookie('_refer', '');
	
//	//ͬ����¼
//	if($_SCONFIG['uc_status']) {
//		include_once S_ROOT.'./uc_client/client.php';
//		$ucsynlogin = uc_user_synlogin($setarr['uid']);
//	} else {
//		$ucsynlogin = '';
//	}
	
	//�ж��û��Ƿ�������ͷ��

       	$_SGLOBAL['db']->query("DELETE FROM " . tname('adminsession') . " WHERE uid='$passport[uid]' OR dateline+1800<'$timestamp'");
        $_SGLOBAL['db']->query("INSERT INTO " . tname('adminsession') . " (uid, ip, dateline, errorcount)
			VALUES ('$passport[uid]', '" . getonlineip() . "', '$_SGLOBAL[timestamp]', '-1')");
        ssetcookie('_refer', '');
        showmessage('login_success', 'admincp.php', 0);
 }
include template(ADMDIR.'/tpl/login');

?>